package com.intexh.quickandroid.module.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.intexh.quickandroid.R;
import com.intexh.quickandroid.base.BaseActivity;
import com.intexh.quickandroid.helper.UserHelper;
import com.intexh.quickandroid.net.Apis;
import com.intexh.quickandroid.net.NetworkManager;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BindPhoneActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.getcode_tv)
    TextView getcodeTv;
    @BindView(R.id.code_et)
    EditText codeEt;
    @BindView(R.id.confirm_btn)
    Button confirmBtn;
    private String mPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_phone);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.back_iv, R.id.getcode_tv, R.id.confirm_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.getcode_tv:
                getCode();
                break;
            case R.id.confirm_btn:
                String code = codeEt.getText().toString();
                if(TextUtils.isEmpty(code)){
                    showToast("请输入验证码");
                    return;
                }
                Map<String,String> params= new HashMap<>();
                params.put("phone",mPhone);
                params.put("captcha",code);
                showProgress();
                NetworkManager.INSTANCE.post(Apis.update_phone, params, new NetworkManager.OnRequestCallBack() {
                    @Override
                    public void onOk(String response) {
                        hideProgress();
                        showToast("修改完成");
                        Intent intent = new Intent();
                        intent.putExtra("phone",mPhone);
                        setResult(RESULT_OK);
                        finish();
                    }

                    @Override
                    public void onError(String errorMessage) {
                        hideProgress();
                        showToast(errorMessage);
                    }
                });
                break;
        }
    }


    private void getCode() {
        mPhone = phoneEt.getText().toString();
        if(TextUtils.isEmpty(mPhone)){
            showToast("请输入手机号");
            return;
        }
        HashMap<String,String> params = new HashMap<>() ;
        params.put("phone", UserHelper.getUser().getMember_mobile()) ;
        params.put("type","4") ;
        showProgress();
        NetworkManager.INSTANCE.post(Apis.getMobileCode, params, new NetworkManager.OnRequestCallBack() {
            @Override
            public void onOk(String response) {
                hideProgress();
                showToast("验证码发送成功，请注意查收");
                countDownReSend(getcodeTv,60);
            }

            @Override
            public void onError(String errorMessage) {
                hideProgress();
                showToast(errorMessage);
            }
        });
    }
}
